package com.woniuxy.controller;

import com.woniuxy.pojo.Dictionary;
import com.woniuxy.service.DictionaryService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.DictionaryVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DictionaryController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping(value = "/dictionarys/list.do")
    public List<Dictionary> selectAllDics() {
        return dictionaryService.findAllDics();
    }

    @GetMapping(value = "/dictionary/selectDicByPid.do")
    public List<Dictionary> queryDicsByPid(int id) {
        return dictionaryService.queryDicsByPid(id);
    }

    @PostMapping(value = "/dictionarys.do")
    public ResultDto AddDictionary(Dictionary dictionary) {
        Dictionary DicResult = this.dictionaryService.findDicByName(dictionary.getName());
        if (DicResult != null) {
            return ResultDto.USER_EXITS;
        } else {
            boolean flag = this.dictionaryService.AddDictionary(dictionary);
            if (flag) {
                return ResultDto.DIC_ADD_SUCCESS;
            } else {
                return ResultDto.DIC_ADD_FAILURE;
            }
        }
    }

    @GetMapping(value ="/dictionarys")
    public DataGridView selectDicsWithPname(DictionaryVo dictionaryVo){
        logger.debug("当前页:" + dictionaryVo.getPage());
        logger.debug("每页的记录数:" + dictionaryVo.getLimit());
        logger.debug("搜索框的信息:" + dictionaryVo.getName());
        return dictionaryService.selectDicsWithPname(dictionaryVo);
    }

    @GetMapping(value = "/dictionarys/deptList.do")
    public List<Dictionary> selectAllDeptsInDic() {
        return dictionaryService.findAllDeptsInDic();
    }

    @GetMapping(value = "/dictionarys/postList.do")
    public List<Dictionary> selectAllPostsInDic() {
        return dictionaryService.findAllPostsInDic();
    }
}
